package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.MapRenderer;
import com.badlogic.gdx.maps.tiled.AtlasTmxMapLoader;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

/**
 * Created by 80002023 on 2016/7/26.
 */
public class MapDemo extends ApplicationAdapter {

    private OrthographicCamera mCamera;
    private MapRenderer mMapRenderer;

//    private TileAtlas atlas;

    private TiledMap map;
    private TextureAtlas atlas;

    @Override
    public void create() {
//        TiledMap map = new TmxMapLoader().load("micro.tmx");
        TiledMap map = new TmxMapLoader().load("map1.tmx");

        FileHandle mapHandle = Gdx.files.internal("map1.tmx");
        FileHandle baseDir = Gdx.files.internal("");
        //初始化地图
//        map = TiledLoader.createMap(mapHandle);
        //初始化图集
//        atlas = new TileAtlas(map, baseDir);
        //初始化地图渲染器
//        tileMapRenderer = new TileMapRenderer(map, atlas, 10, 10);

        map = new TmxMapLoader().load("map1.tmx");

//        atlas = new AtlasTmxMapLoader(map, baseDir);
        // 镜头需要设定宽高
//        mCamera = new OrthographicCamera(800, 480);
        mCamera = new OrthographicCamera(480, 800);
        // 这里默认1个unit单元=1个pixels像素
        // OrthogonalTiledMapRenderer还可以设置单元和像素之间的比例
        mMapRenderer = new OrthogonalTiledMapRenderer(map);
        // 设置镜头的宽和高分别是800,480单元,y方向向上和镜头中间位置
        mCamera.setToOrtho(false, 480, 800);
        mCamera.update();
    }

    @Override
    public void render() {

        // 镜头的更新与设置矩阵到SpriteBatch
        mCamera.update();
        // 设置背景
        Gdx.gl.glClearColor(0.7f, 0.7f, 1.0f, 1);
        // 清屏
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        // 设置摄像机
        mMapRenderer.setView(mCamera);
        // 地图绘制
        mMapRenderer.render();
    }
}
